package com.example.basketVoice

import android.content.Context
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

object FileUtils {
    fun copyAssets(context: Context, srcPath: String, destPath: String) {
        val assetManager = context.assets
        try {
            val files = assetManager.list(srcPath) ?: return

            val destDir = File(destPath)
            if (!destDir.exists()) destDir.mkdirs()

            files.forEach { file ->
                if (assetManager.list("$srcPath/$file")?.isNotEmpty() == true) {
                    // 处理子目录
                    copyAssets(context, "$srcPath/$file", "$destPath/$file")
                } else {
                    // 处理文件
                    assetManager.open("$srcPath/$file").use { input ->
                        FileOutputStream(File(destDir, file)).use { output ->
                            input.copyTo(output)
                        }
                    }
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}